package com.wyh.service.serviceImpl.feemanagementserviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.wyh.domain.dto.feemanagementdto.*;
import com.wyh.domain.po.feemanagementpo.Billes;
import com.wyh.domain.po.feemanagementpo.Billdetailes;
import com.wyh.domain.po.feemanagementpo.InvoiceHeaderes;
import com.wyh.domain.po.feemanagementpo.Invoicerecordes;
import com.wyh.mapper.feemanagementmapper.BillMapper;
import com.wyh.mapper.feemanagementmapper.BillsMapper;
import com.wyh.service.feemanagementservice.BillsService;
import com.wyh.utils.feemanagementutils.PdfPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;



@Service
public class BillsServiceImpl extends ServiceImpl<BillsMapper, Billdetailes> implements BillsService {

    @Autowired
    private BillsMapper billsMapper;
    @Autowired
    private BillMapper billMapper;

    /**
     * 周期性账单分页查询-待通知
     * @param billPageQueryDTO
     * @return
     */
    @Override
    public PageResultDTO pageQuery(BillPageQueryDTO billPageQueryDTO,String account) {
        PageHelper.startPage(billPageQueryDTO.getPage(), billPageQueryDTO.getPageSize());

        Page<BillDTO> page = billsMapper.pageQuery(billPageQueryDTO,account);

        long total = page.getTotal();
        List<BillDTO> records = page.getResult();
        return new PageResultDTO(total,records);

    }

    /**
     * 账单
     * @param id
     * @return
     */
    @Override
    public List<BillDetailDTO> getBillDeatilById(Integer id) {

        return billsMapper.getBillDetailById(id);
    }

    /**
     * 账单详情查询
     * @param id
     * @return
     */
    @Override
    public List<BillDetailbDTO> getBillDeatilByIdb(Integer id) {
        return billsMapper.getBillDetailByIdb(id);
    }

    @Override
    public List<Billdetailes> getBillDeatilsByBillIds(Integer id) {
        List<Billdetailes> billdetails = lambdaQuery().eq(Billdetailes::getBillid, id).list();
        if(CollectionUtil.isNotEmpty(billdetails)){
            return billdetails;
        }
        return Collections.emptyList();
    }

    @Override
    public List<Billes> getBillsByResourceid(String resourceStringId) {
        List<Billdetailes> billdetails = lambdaQuery().eq(Billdetailes::getBillingresourcesid, resourceStringId).list();
        Map<Integer, List<Billdetailes>> collect = billdetails.stream().collect(Collectors.groupingBy(Billdetailes::getBillid));
        Set<Integer> integers = collect.keySet();
        if(CollectionUtil.isNotEmpty(integers)){
            List<Billes> billes = billMapper.selectBatchIds(integers);
            if(CollectionUtil.isNotEmpty(billes)){
                return billes;
            }
        }
        return Collections.emptyList();
    }


    @Override
    public List<Billes> getBillsByResourceidPage(IPage page, String resourceStringId) {
        //根据资源id查询账单详情
        IPage<Billdetailes> page1 = lambdaQuery().eq(Billdetailes::getBillingresourcesid, resourceStringId).page(page);
        //拿到分页后的账单详情数据
        List<Billdetailes> billdetails = page1.getRecords();
        Map<Integer, List<Billdetailes>> collect = billdetails.stream().collect(Collectors.groupingBy(Billdetailes::getBillid));
        Set<Integer> integers = collect.keySet();
        //账单详情的id查询账单
        List<Billes> billes = billMapper.selectBatchIds(integers);
        if(CollectionUtil.isNotEmpty(billes)){
            return billes;
        }
        return Collections.emptyList();
    }


    @Override
    public List<Billdetailes> getBillDeatilsByids(List<Integer> ids) {
        List<Billdetailes> billdetails = baseMapper.selectBatchIds(ids);
        if(CollectionUtil.isNotEmpty(billdetails)){
            return billdetails;
        }
        return Collections.emptyList();
    }

    /**
     * 获取开票记录的数据
     * @param invoicingDTO
     * @return
     */
    @Override
    public InvoicingReDTO getInvoicerecords(InvoicingDTO invoicingDTO,String account) {

        InvoicingReDTO invoicingReDTO = new InvoicingReDTO();

        invoicingReDTO.setSerialnumber(null);
        invoicingReDTO.setNoticenumber(null);
        invoicingReDTO.setInvoicingtype(billsMapper.getInvoiceName(invoicingDTO.getName()));
        invoicingReDTO.setInvoicecode(null);
        invoicingReDTO.setInvoicenumber(null);
        invoicingReDTO.setSalesside(billsMapper.getSalesside(account));
        invoicingReDTO.setOwner(null);
        invoicingReDTO.setBuyer(null);
        invoicingReDTO.setInvoicingtime(null);
        invoicingReDTO.setAmount(billsMapper.getAccount(invoicingDTO.getId()));
        invoicingReDTO.setState(null);
        if(invoicingDTO.getName() == "纸质收据"){
            invoicingReDTO.setType(1);
        }
        else {
            invoicingReDTO.setType(0);
        }

        invoicingReDTO.setBillid(invoicingDTO.getId());




        return invoicingReDTO;
    }

    /**
     * 根据账单id获取发票抬头表数据
     * @param id
     * @return
     */
    @Override
    public InvoiceHeaderes getHeaderes(Integer id) {

        return billsMapper.getHeaderes(id);

    }

    /**
     * 返回当前用户所管辖的楼盘名称列表
     * @param account
     * @return
     */
    @Override
    public List<String> getPD(String account) {
        return billsMapper.getPD(account);
    }

    /**
     * 为开票记录表插入数据
     * @param invoicingReDTO
     */
    @Override
    public void invoicing(InvoicingReDTO invoicingReDTO) {
        billsMapper.invoicing(invoicingReDTO);
    }

    /**
     * 获取账单打印-费用明细数据
     * @param id
     * @return
     */
    @Override
    public List<ExpenseDetails> getExpenseDetails(Integer id) {
        return billsMapper.getExpenseDetails(id);
    }

    @Override
    public String getNoticenumber(Integer id) {
        return billsMapper.getNoticenumber(id);
    }

    @Override
    public Date getAccounttime(Integer id) {
        return billsMapper.getAccounttime(id);
    }

    @Override
    public String getRemarks() {
        return billsMapper.getRemarks();
    }

    @Override
    public ReceiptDTO getReceipte(Integer id, String account) {
        return billsMapper.getReceipt(id,account);
    }

//    @Override
//    public List<BillDTO> getAccount(Integer id) {
//        return billsMapper.getAccounts(id);
//    }



    /**
     * 根据客户id获取客户邮箱
     * @param ids
     * @return
     */
    @Override
    public String[] getEmails(Integer[] ids) {
        return billsMapper.getEmails(ids);
    }

    /**
     * 获取通知客户的通知方式(是邮件还是短信，异或二者都要)
     * @return
     */
    @Override
    public int getSendMethod() {
        return billsMapper.getSendMehod();
    }

    @Override
    public String[] getPhones(Integer[] ids) {
        return billsMapper.getPhones(ids);
    }

    /**
     * 在通知用户后改变账单通知状态为已通知
     * @param ids
     */
    @Override
    public void changeNotificationstatus(Integer[] ids) {
         billsMapper.changeNotificationstatus(ids);
    }


    @Override
    public List<Billdetailes> getBillDeatilsByRecource(Integer resourceid) {
        List<Billdetailes> billdetails = lambdaQuery().eq(Billdetailes::getBillingresourcesid, resourceid.toString()).list();
        if(CollectionUtil.isNotEmpty(billdetails)){
            return billdetails;
        }
        return Collections.emptyList();
    }


    @Override
    public List<PdfDTO> gettemplatetxt(Integer[] ids) {
        List<PdfDTO> pdfDTOS =  billsMapper.gettemplatetxt(ids);
        return pdfDTOS;
    }


    /**
     * 账单详情查询-票据信息
     * @param id
     * @return
     */
    @Override
    public List<InvoiceDTO> getBillDeatilByIds(Integer id) {
        return billsMapper.getBillDetailByIds(id);
    }

    /**
     * 退回账单
     */
    @Override
    public void returnBill(ReturnBillDTO returnBillDTO) {
        billsMapper.returnBill(returnBillDTO);
    }


    /**
     * 周期性账单分页查询-已通知
     * @param billPageQueryDTO
     * @return
     */
    @Override
    public PageResultTwoDTO pageTwoQuery(BillPageQueryDTO billPageQueryDTO,String account) {
        PageHelper.startPage(billPageQueryDTO.getPage(), billPageQueryDTO.getPageSize());

        Page<BillNoticeDTO> page = billsMapper.pageTwoQuery(billPageQueryDTO,account);

        long total = page.getTotal();
        List<BillNoticeDTO> records = page.getResult();
        return new PageResultTwoDTO(total,records);
    }

    /**
     * 临时账单分页查询
     * @param billPageTpQueryDTO
     * @return
     */
    @Override
    public PageResultTpDTO pageTpQuery(BillPageTpQueryDTO billPageTpQueryDTO,String account) {
        PageHelper.startPage(billPageTpQueryDTO.getPage(), billPageTpQueryDTO.getPageSize());

        Page<BillTpDTO> page = billsMapper.pageTpQuery(billPageTpQueryDTO,account);

        long total = page.getTotal();
        List<BillTpDTO> records = page.getResult();
        return new PageResultTpDTO(total,records);
    }

}
